package ninja.awesome.game;

import java.util.ArrayList;
import java.util.List;

import ninja.awesome.framework.Game;
import ninja.awesome.framework.Graphics;
import ninja.awesome.framework.Input.TouchEvent;
import ninja.awesome.framework.Screen;
import ninja.awesome.framework.control.GameButton;
import ninja.awesome.framework.control.GameButton.ButtonListener;
import ninja.awesome.framework.impl.AndroidGame;
import android.graphics.Color;

public class MainMenuScreen extends Screen implements ButtonListener {

	private static final String TAG = MainMenuScreen.class.getSimpleName();
	public int buttonTop = 550;
	public int buttonWidth = 340;
	public int buttonHeight = 85;

	List<GameButton> buttons = new ArrayList<GameButton>();
	private GameButton mPlayButton;
	private GameButton mHighScoresButton;
	private GameButton mDonateButton;

	public MainMenuScreen(Game game) {
		super(game);
		mPlayButton = getMenuButton("Play", 80, buttonTop, buttonWidth, buttonHeight);
		mPlayButton.setBackgroundImage(Assets.btnMenuPlay);
		mPlayButton.setPressedImage(Assets.btnMenuPlayPressed);
		buttons.add(mPlayButton);
		mHighScoresButton = getMenuButton("High Scores", 470, buttonTop, buttonWidth, buttonHeight);
		mHighScoresButton.setBackgroundImage(Assets.btnMenuHighScores);
		mHighScoresButton.setPressedImage(Assets.btnMenuHighScoresPressed);
		buttons.add(mHighScoresButton);
		mDonateButton = getMenuButton("Donate", 855, buttonTop, buttonWidth, buttonHeight);
		mDonateButton.setBackgroundImage(Assets.btnMenuDonate);
		mDonateButton.setPressedImage(Assets.btnMenuDonatePressed);
		buttons.add(mDonateButton);
	}

	private GameButton getMenuButton(String text, int x, int y, int width, int height) {
		GameButton result = new GameButton(game);
		result.setText(text);
		result.setBackgroundColor(Color.WHITE);
		result.setTextSize(60);
		result.setPosition(x, y, width, height);
		result.setButtonListener(this);
		return result;
	}

	@Override
	public void update(float deltaTime) {
		// game.setScreen(new MainGameScreen(game));
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		if (touchEvents.size() != 0) {
			for (GameButton b : buttons) {
				b.update(deltaTime, touchEvents);
			}
		}

	}

	@Override
	public void paint(float deltaTime) {
		Graphics g = game.getGraphics();
		g.clearScreen(Color.BLACK);
		g.drawImage(Assets.menuBackground, 0, 0);
		g.drawImage(Assets.menuBanner, 330, 25);
		for (GameButton b : buttons) {
			b.paint(deltaTime);
		}
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
	}

	@Override
	public void backButton() {
		try {
			Assets.theme.stop();
			Assets.theme.dispose();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		android.os.Process.killProcess(android.os.Process.myPid());

	}

	@Override
	public void onExecute(GameButton button) {
		if (button == mPlayButton) {
			// game.setScreen(new MainGameScreen(game));
			((AndroidGame) game).runOnUiThread(new Runnable() {

				@Override
				public void run() {
					((AndroidGame) game).processPlay();
				}
			});
		} else if (button == mHighScoresButton) {
			game.setScreen(new HighScoresScreen(game));
		} else if (button == mDonateButton) {
			game.setScreen(new DonateScreen(game));
		}
	}

	@Override
	public void onPressed(GameButton button) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onRealeased(GameButton button) {
		// TODO Auto-generated method stub

	}
}